<?php

namespace backend\modules\logs\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\logs\models\ApiSendToBankLog;

/**
 * ApiSendToBankLoggSearch represents the model behind the search form about `backend\modules\logs\models\ApiSendToBankLog`.
 */
class ApiSendToBankLoggSearch extends ApiSendToBankLog
{
    public $idno;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            ['idno', 'string'],
            [['header_data', 'get_data', 'post_data', 'other_data', 'return_content', 'time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ApiSendToBankLog::find()->orderBy(['id'=>SORT_DESC]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
//            'time' => $this->time,
        ]);

        $query->andFilterWhere(['like', 'header_data', $this->header_data])
            ->andFilterWhere(['like', 'get_data', $this->get_data])
            ->andFilterWhere(['like', 'post_data', $this->post_data])
            ->andFilterWhere(['like', 'other_data', $this->other_data])
            ->andFilterWhere(['like', 'return_content', $this->return_content]);
//        $query = $query->andFilterWhere(['like', 'post_data', $this->idno]);
        if ( ! is_null($this->time) && strpos($this->time, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->time);
            $query->andFilterWhere(['between', 'time', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }
        return $dataProvider;
    }
}
